//
//  CTCActivityDetailVC.m
//  CTC
//
//  Created by LXL on 2017/6/9.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "CTCActivityDetailVC.h"
#import "CTCActivityModel.h"
#import "CTCActivityIntroCell.h"
#import "CTCActivityDetailCell.h"

@interface CTCActivityDetailVC ()<CTCActivityIntroCellDelegate>
@property (strong, nonatomic) CTCActivityModel *detailModel;
@property (strong, nonatomic) CTCActivityIntroCell *activityIntroCell;
/** 文章详细内容 */
@property (nonatomic, copy) NSString *activityIntroStr;
@end

@implementation CTCActivityDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"活动详情";
    
    self.tableView.backgroundColor = HEXCOLOR(0xe6e6e6);
    
     [self.tableView registerNib:[UINib nibWithNibName:@"CTCActivityIntroCell" bundle:nil] forCellReuseIdentifier:@"CTCActivityIntroCell"];
    
     [self.tableView registerNib:[UINib nibWithNibName:@"CTCActivityDetailCell" bundle:nil] forCellReuseIdentifier:@"CTCActivityDetailCell"];

    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
     [self loadWebViewHtml];
    
//     [self loadDetailData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.activityIntroCell stopLoading];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
     [self.activityIntroCell stopLoading];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row) {
        CGSize size = [self.activityIntroCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
      return    size.height + 1 ;
    }
    
   return  [tableView fd_heightForCellWithIdentifier:@"CTCActivityDetailCell" configuration:^(CTCActivityDetailCell *cell) {
        cell.activityModel = self.activityModel;
    }];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.row) {
        CTCActivityIntroCell * cell =[tableView dequeueReusableCellWithIdentifier:@"CTCActivityIntroCell" forIndexPath:indexPath];
        self.activityIntroCell = cell;
        self.activityIntroCell.delegate = self;
        self.activityIntroCell.html = self.activityIntroStr;
        //    self.activityIntroCell.ac = self.articleModel;
        return self.activityIntroCell;
        
    }
    
    CTCActivityDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CTCActivityDetailCell" forIndexPath:indexPath];
    cell.activityModel = self.activityModel;
    
    return cell;
    
}


- (void)activityIntroCell:(CTCActivityIntroCell *)cell heightDidChange:(CGFloat)height {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        
    });
}

- (void)loadWebViewHtml {
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
    [self.tableView reloadData];
    parames[@"uuid"] = self.activityModel.uuid;

    [RequestTool POST:kActivityDetailUrl parameters:parames progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        DeLog(@"活动详情responseObject： %@", responseObject);
        if ([responseObject[@"code"]  integerValue] == 1) {
            [MBProgressHUD hideHUD];
            self.activityIntroStr = responseObject[@"data"][@"content"];
            [self.tableView reloadData];
            
        }  else if ([responseObject[@"code"] intValue] == 2) {
            [MBProgressHUD showAnimationHUD];
            CTCLoginViewController *vc = [CTCLoginViewController viewControllerFromStoryboard];
            MPWeakSelf(self);
            vc.loginSuccess = ^(){
                 [weakself loadWebViewHtml];
            };
            [self presentViewController:vc animated:YES completion:nil];
            
        } else {
            [MBProgressHUD showErrorMessage:responseObject[@"des"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"load html error  -- %@", error);
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力，请检查网络"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
    }];

}


@end
